/*
 * Copyright 2022 Bytedance Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.css.service.deploy.worker;

import com.bytedance.css.common.protocol.ShuffleMode;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public interface FileWriter {

  // For Hdfs file, it might not work, just return null would be fine
  ArrayList<Long> getChunkOffsets();

  // write ByteBuf data into target file
  default void write(ByteBuf data, int mapperId) throws IOException {
    write(data, mapperId, false);
  }

  // write ByteBuf data into target file
  void write(ByteBuf data, int mapperId, boolean ignoreRotate) throws IOException;

  // close file and return flushed length
  long close() throws IOException;

  // close file and delete file
  void destroy();

  // for test only, set IOException into notifier
  void setException();

  // for test only, set IOException into flush queue.
  void setSubmitQueueFullException(boolean exception);

  // for test only, get CompositeByteBuf which hold by file writer.
  CompositeByteBuf getCompositeByteBuf();

  void checkException() throws IOException;
  boolean shouldRotate();

  void incrementPendingWrites();
  void decrementPendingWrites();

  ShuffleMode getShuffleMode();
  String getFilePath();
  long getFileLength();
  File getFile();
}
